﻿using System.Net.Http.Headers;
using System.Net.Security;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Reflection.PortableExecutable;

namespace MachineVision.Service
{
    public class HttpXmlClient
    {
        public static string Post(string url, Dictionary<string, string> paramsparams, Dictionary<string, string> headers)
        {
            var client = new HttpClient();
            if (url.StartsWith("https"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnRemoteCertificateValidationCallback);
            }
            var values = new List<KeyValuePair<string, string>>();
            foreach (KeyValuePair<string, string> kv in paramsparams)
            {
                values.Add(kv);
            }

            var content = new FormUrlEncodedContent(values);
            if (null != headers)
            {
                foreach (KeyValuePair<string, string> kv in headers)
                {
                    client.DefaultRequestHeaders.Add(kv.Key, kv.Value);
                }
            }
            HttpResponseMessage response;
            try
            {
                response = client.PostAsync(url, content).Result;
            }
            catch
            {
                return null;
            }
            string result = "";
            var statusCode = response.StatusCode.ToString();
            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsStringAsync().Result;
                return result;
            }
            return result;
        }

        public static string Post(string url, string str, Dictionary<string, string> headers)
        {
            var client = new HttpClient();
            if (url.StartsWith("https"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnRemoteCertificateValidationCallback);
            }
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var content = new StringContent(str, Encoding.UTF8, "application/json");
            if (null != headers)
            {
                foreach (KeyValuePair<string, string> kv in headers)
                {
                    client.DefaultRequestHeaders.Add(kv.Key, kv.Value);
                }
            }
            HttpResponseMessage response;
            try
            {
                response = client.PostAsync(url, content).Result;
            }
            catch
            {
                return null;
            }
            string result = "";
            var statusCode = response.StatusCode.ToString();
            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsStringAsync().Result;
                return result;
            }
            return result;
        }

        public static string GetHttpResponse(string url, Dictionary<string, string> headers)
        {
            var client = new HttpClient();
            if (url.StartsWith("https"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnRemoteCertificateValidationCallback);
            }
            if (null != headers)
            {
                foreach (KeyValuePair<string, string> kv in headers)
                {
                    client.DefaultRequestHeaders.Add(kv.Key, kv.Value);
                }
            }
            HttpResponseMessage response;
            try
            {
                response = client.GetAsync(url).Result;
            }
            catch
            {
                return null;
            }
            string result = "";
            var statusCode = response.StatusCode.ToString();
            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsStringAsync().Result;
                return result;
            }
            return result;
        }


            // 忽略证书认证错误处理的函数
            private static bool OnRemoteCertificateValidationCallback(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;  // 认证正常，没有错误
        }
    }
}
